<div class="layui-fluid">
    <div class="layui-breadcrumb jy-breadcrumb">

    </div>
    <div class="layui-card">
        <div class="layui-form layui-card-header layuiadmin-card-header-auto" lay-filter="LAY-list-form">
            <div class="layui-form-item">
                <div class="layui-inline">
                    <label class="layui-form-label">SKU</label>
                    <div class="layui-input-block">
                        <input type="text" name="sku" placeholder="多个用空格或逗号隔开" autocomplete="off" class="layui-input">
                    </div>
                </div>
                <div class="layui-inline">
                    <label class="layui-form-label">SPU</label>
                    <div class="layui-input-block">
                        <input type="text" name="spu" placeholder="多个用空格或逗号隔开" autocomplete="off" class="layui-input">
                    </div>
                </div>
                <div class="layui-inline">
                    <label class="layui-form-label">导入人员</label>
                    <div class="layui-input-block">
                      <select name="importoperator" xm-select="user1" xm-select-search="/index/User/getUserByName" xm-select-radio=""></select>
                    </div>
                </div>
                <div class="layui-inline">
                    <label class="layui-form-label">编辑/图片人员</label>
                    <div class="layui-input-block">
                      <select name="operator" xm-select="user2" xm-select-search="/index/User/getUserByName" xm-select-radio=""></select>
                    </div>
                </div>
                <div class="layui-inline">
                    <label class="layui-form-label">分派状态</label>
                    <div class="layui-input-block">
                        <select name="assignstatus">
                            <option value=""></option>
                            <option value="1">待分派</option>
                            <option value="2">已分派</option>
                        </select>
                    </div>
                </div>
                <div class="layui-inline">
                    <label class="layui-form-label">制作状态</label>
                    <div class="layui-input-block">
                        <select name="manufacture_status" >
                            <option value="">请选择</option>
                            <option value="1">待确定</option>
                            <option value="2">待制作</option>
                            <option value="5">已制作</option>
                        </select>
                    </div>
                </div>
                <div class="layui-inline ">
                    <label class="layui-form-label">确定日期</label>
                    <div class="layui-input-block layui-jy-date">
                        <input type="text" class="layui-input" id="date" name="assign_time" placeholder="">
                    </div>
                </div>
                <div class="layui-inline ">
                    <label class="layui-form-label">完成日期</label>
                    <div class="layui-input-block layui-jy-date">
                        <input type="text" class="layui-input" id="date1" name="finish_time" placeholder="">
                    </div>
                </div>
                <div class="layui-inline">
                    <label class="layui-form-label"></label>
                    <button class="layui-btn search" lay-submit lay-filter="LAY-list-search">查询</button>
                    <button type="button" class="tabreset layui-btn layui-btn-primary">重置</button>
                    <input type="hidden" id='tabtype' value='1'>
                </div>
            </div>
        </div>
        <div class="layui-tab" lay-filter="showInfo" style="background-color:#FFFFFF">
            <ul class="layui-tab-title">
                <li class="layui-this" lay-id="1">编辑任务</li>
                <li lay-id="2">图片任务</li>
            </ul>
            <div class="layui-tab-content" style="background-color:#FFFFFF">
                <div class="layui-tab-item layui-show">
                    <div class="layui-card-body">
                        <div class="layui-form" lay-filter="batchOp-form">
                            <div class="layui-form-item">
                                <div class="layui-inline">
                                    <select lay-filter="batchOp" >
                                        <option value="0" selected>批量操作</option>
                                        <option value="1">导出数据</option>
                                        <option value="2">导入数据</option>
                                        <option value="3">批量确定</option>
                                        <option value="4">批量取消</option>
                                    </select>
                                </div>
                            </div>
                        </div>
                        <table id="LAY-list-normal1"  lay-filter="LAY-list-normal1"></table>
                        <!-- 行内工具 -->
                        <script type="text/html" id="operation1">
                            {{# if(d.manufacture_statusid==1 && d.operator!=''){ }}
                            <a class="layui-btn layui-btn-normal layui-btn-xs" lay-event="affirm" href="javascript:;">确定</a>
                            {{# } }}
                            <br/><br/>
                            {{# if(d.operator==''){ }}
                            <a class="layui-btn layui-btn-normal layui-btn-xs" lay-event="assign" href="javascript:;">分派</a>
                            {{# } }}
                            <br/><br/>
                            {{# if(d.cancel==0 && d.manufacture_statusid < 3){ }}
                            <a class="layui-btn layui-btn-normal layui-btn-xs" lay-event="cancel" href="javascript:;">取消</a>
                            {{# } }}
                        </script>
                    </div>
                </div>
                <div class="layui-tab-item">            
                    <div class="layui-card-body">
                        <div class="layui-form" lay-filter="batchOp-form">
                            <div class="layui-form-item">
                                <div class="layui-inline">
                                    <select lay-filter="batchOp" >
                                        <option value="0" selected>批量操作</option>
                                        <option value="1">导出数据</option>
                                        <option value="2">导入数据</option>
                                        <option value="3">批量确定</option>
                                        <option value="4">批量取消</option>
                                    </select>
                                </div>
                            </div>
                        </div>
                        <table id="LAY-list-normal2"  lay-filter="LAY-list-normal2"></table>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>
<style>
    #weight:after {
        content: 'g';
        position: absolute;
        top: 0px;
        right: 10px;
    }
    .category_id .xm-select-title{
        width: 500px;
    }
    .layui-card .layui-table-cell {
        height: auto;
        overflow: visible;
        text-overflow: inherit;
        white-space: normal;
        line-height: normal;
    }
</style>
<script type="text/html" id="toolbar">
    <div class="layui-btn-group fl layui-btn-group-jy">
        <a  class="layui-btn layui-btn-primary layui-btn-sm layui-icon layui-icon-ok" lay-event="checked">&nbsp;勾选确定</a>
    </div>
</script>
<script>
    layui.use(['admin', 'table', 'form', 'toolJ', 'formSelects', 'laydate', 'laytpl','upload', 'element'], function () {
        var $ = layui.$
            , admin = layui.admin
            , view = layui.view
            , table = layui.table
            , laytpl = layui.laytpl
            , upload = layui.upload
            , laydate = layui.laydate
            , formSelects = layui.formSelects
            , toolJ = layui.toolJ
            , element = layui.element
            , form = layui.form;
        //日期
        laydate.render({
            elem: '#date'
            , type: 'date'
            , range: true
        });
        laydate.render({
            elem: '#date1'
            , type: 'date'
            , range: true
        });
        
        form.render(null);
        formSelects.render();

        $('.tabreset').click(function(){
            var url = window.location.toString();
            location = url.split('#')[0]+'#'+url.split('#')[1]+'#'+$("#tabtype").val()+Math.ceil(Math.random()*10);
        });

        table.on('toolbar(LAY-list-normal1)', function (obj) {
            //选中的数据
            var checkStatus = table.checkStatus('LAY-list-normal1')
                , data = checkStatus.data;
            var id=[];
            for(var i in data ){
                id.push(data[i].id);
            }
            switch (obj.event) {
                case 'checked':
                    if(id.length<=0){
                        toolJ.error('请勾选再操作');
                        return false;
                    }
                    //批量确认
                    switch ($("#tabtype").val()) {
                        case '1':
                            var type='edit';
                            break;
                        case '2':
                            var type='img';
                            break;
                    }
                    var field ={status:'ok',type:type,id:id};
                    toolJ.ajax({"data": field, "url": "/index/Oldproductmanage/updateAffirmCancelAuditLogicCheck"});
                    break;
            }
        })
        table.on('toolbar(LAY-list-normal2)', function (obj) {
            //选中的数据
            var checkStatus = table.checkStatus('LAY-list-normal2')
                , data = checkStatus.data;
            var id=[];
            for(var i in data ){
                id.push(data[i].id);
            }
            switch (obj.event) {
                case 'checked':
                    if(id.length<=0){
                        toolJ.error('请勾选再操作');
                        return false;
                    }
                    //批量确认
                    switch ($("#tabtype").val()) {
                        case '1':
                            var type='edit';
                            break;
                        case '2':
                            var type='img';
                            break;
                    }
                    var field ={status:'ok',type:type,id:id};
                    toolJ.ajax({"data": field, "url": "/index/Oldproductmanage/updateAffirmCancelAuditLogicCheck"});
                    break;
            }
        })
        var url = window.location.toString();
        var tablayid = url.split('#')[2];
        if(tablayid){
            tablayid = tablayid.slice(0, 1);
            element.tabChange('showInfo', tablayid);
            $("#tabtype").val(tablayid);
            $('.searchbtn'+tablayid).click();
        }

        element.on('tab(showInfo)', function () {
            var layid = $(this).attr('lay-id');
            $("#tabtype").val(layid);
            $('.search').click();
        });
        //批量操作
        form.on('select(batchOp)', function(data){
            switch (data.value) {
                case '1':
                    //列表导出
                    switch ($("#tabtype").val()) {
                        case '1':
                            var url = '/index/Oldproductmanage/exportEditTaskList'; //编辑任务
                            break;
                        case '2':
                            var url = '/index/Oldproductmanage/exportImgTaskList'; //图片任务
                            break;
                    }

                    var data = form.val("LAY-list-form");
                    toolJ.ajax({"data": data, 'url': url});
                    break;
                case '2':
                    //列表导入
                    switch ($("#tabtype").val()) {
                        case '1':
                            var url = '/index/Oldproductmanage/uploadEditTaskListInfo';
                            var template = '/attachment/importcvs/edittask.csv'; 
                            break;
                        case '2':
                            var url = '/index/Oldproductmanage/uploadImgTaskListInfo';
                            var template = '/attachment/importcvs/imgtask.csv'; 
                            break;
                    }
                    toolJ.upload(url, template);
                    break;
                case '3':
                    //批量确认
                    switch ($("#tabtype").val()) {
                        case '1':
                            var url = '/index/Oldproductmanage/uploadEditBatchAffirm';
                            var template = '/attachment/importcvs/editimgbatch.csv'; 
                            break;
                        case '2':
                            var url = '/index/Oldproductmanage/uploadImgBatchAffirm';
                            var template = '/attachment/importcvs/editimgbatch.csv'; 
                            break;
                    }
                    toolJ.upload(url, template);
                    break;
                case '4':
                    //批量取消
                    switch ($("#tabtype").val()) {
                        case '1':
                            var url = '/index/Oldproductmanage/uploadEditBatchCancel';
                            var template = '/attachment/importcvs/editimgbatch.csv'; 
                            break;
                        case '2':
                            var url = '/index/Oldproductmanage/uploadImgBatchCancel';
                            var template = '/attachment/importcvs/editimgbatch.csv'; 
                            break;
                    }
                    toolJ.upload(url, template);
                    break;

            }
            // data.elem.value=0;
            form.render(null, 'batchOp-form');
            layui.$("div[lay-filter='batchOp-form'] dd[lay-value=0]").remove();
        });
        viewTable();
        layui.form.on('submit(LAY-list-search)', function (data) {
            var field = data.field;
            viewTable(field);
        });
        function viewTable(field = {}) {
            field.old = 1;
            var url='';
            if($("#tabtype").val() == 1){
                field.type = 'edit';
                var param = [
                    {type:'checkbox'},
                    {field: 'skuinfo', title: 'SKU信息'}
                    , {field: 'editdatainfo', title: '制作信息'}
                    , {field: 'editstaffinfo', title: '人员信息'}
                    , {field: 'timeinfo', title: '时间信息'}
                    , {field: 'manufacture_status', title: '制作状态', width:90}
                    , {field: 'audit_remarks', title: '制作备注'}
                    , {fixed: 'right', title:'操作', toolbar: '#operation1', width:60}
                ];
                 url='/index/Oldproductmanage/getOldProductAssignList';
            }else if($("#tabtype").val() == 2){
                field.type = 'img'
                var param = [
                    {type:'checkbox'},
                    {field: 'skuinfo', title: 'SKU信息'}
                    , {field: 'imgdatainfo', title: '资料信息'}
                    , {field: 'imgmakeinfo', title: '制作信息'}
                    , {field: 'imgstaffinfo', title: '人员信息'}
                    , {field: 'timeinfo', title: '时间信息'}
                    , {field: 'manufacture_status', title: '制作状态', width:90}
                    , {field: 'audit_remarks', title: '制作备注'}
                    , {fixed: 'right', title:'操作', toolbar: '#operation1', width:60}
                ];
                 url='/index/Oldproductmanage/getOldProductAssignListImg';
            }

            var ele = 'LAY-list-normal' + $("#tabtype").val();
            table.render({
                elem: "#" + ele
                , url: url
                ,toolbar: '#toolbar'
                ,defaultToolbar:[]
                , where: field
                , cols: [param]
                , page: true
                , limit: 50
                ,text:{none: '暂无相关数据!' }
            });
            //监听行内操作
            table.on('tool('+ele+')', function(obj){
                // debugger;
                var data = obj.data;
                var type = $("#tabtype").val() == 2?'img':'edit';
                switch (obj.event) {
                    case 'affirm':
                        affirmcancel(type,data,'affirm');
                        break;
                    case 'assign':
                        assign(type,data);
                        break;
                    case 'cancel':
                        affirmcancel(type,data,'cancel');
                        break;
                }
            });
        }

        function assign(type,data){
            var title = type == 'img'?'图片':'编辑';
            layer.open({
                type: 5
                ,title: title
                ,area: ['450px', '210px']
                ,shade: 0.8
                ,id: 'LAY_assign' 
                ,btn: ['保存', '取消']
                ,btn1: function(index, layero){
                    var operator = $("input[name=editimgoperator]").val();
                    if(operator == ''){
                       layer.msg(title+'人员不可为空', {"icon": 2});
                       return false; 
                    } 
                    var obj = {};
                    obj.url = '/index/Oldproductmanage/updateAssignInfo';
                    obj.data ={id:data.id,operator:operator,type:type};
                    toolJ.ajax(obj);
                    return false;
                }
                ,content: '<div class="layui-card layui-form" lay-filter="LAY-list-form3"><div class="layui-form-item"><label class="layui-form-label">SKU：</label><div class="layui-input-block"><input type="text" name="assign_sku" value="'+data.sku+'" autocomplete="off" placeholder="" class="layui-input gray" style="width:180px" disabled=disabled></div></div><div class="layui-inline"><label class="layui-form-label">'+title+'人员：</label><div class="layui-input-block" style="padding: 0px 0px 10px 0px"><select name="editimgoperator" xm-select="user10" xm-select-search="/index/User/getUserByName" xm-select-radio=""></select></div></div></div>'
                ,success: function(layero){
                    form.render(null, 'LAY-list-form3');
                    formSelects.render();
                }
            });
        }

        function affirmcancel(type,data,status){
            var title = status == 'cancel'?'取消':'确定';
            layer.open({
                type: 1
                ,title: title
                ,area: ['230px', '150px']
                ,shade: 0.8
                ,id: 'LAY_layuipro' //设定一个id，防止重复弹出
                ,btn: ['确定', '取消']
                ,btn1: function(index, layero){
                    var obj = {};
                    obj.url = '/index/Oldproductmanage/updateAffirmCancelAudit';
                    obj.data ={id:data.id,sku:data.sku,type:type,status:status};
                    toolJ.ajax(obj);
                    return false;
                }
                ,content: '<div class="layui-card" style="padding: 10px 30px 10px 60px" >是否确认'+title+'？</div>'
                ,success: function(layero){
                    form.render(null, 'LAY-list-form1');
                }
            });
        }
        layui.$("div[lay-filter='batchOp-form'] dd[lay-value=0]").remove();
    });
</script>